/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RemoveDuplicates.java
 *
 * Created on Jan 3, 2011, 2:42:59 PM
 */

package ResultEditor.Annotations.Opt;

import java.awt.Dimension;
import java.util.Vector;

/**
 *
 * @author leng
 */
public class RemoveDuplicates extends javax.swing.JFrame {
    protected UserInterface.GUI gui;
    protected int __max=0;

    /** Creates new form RemoveDuplicates */
    public RemoveDuplicates(UserInterface.GUI gui) {
        this.gui = gui;
        initComponents();

        //set size dialog attributes, such as size and position.
        setDialog();

        // set min and max value by return value from
        // ResultEditor.Annotations.Depot.getSizeofallannotations()
        setProgressBar();

        this.update(this.getGraphics());

        // starting checking and comparing
        new Thread(){
                @Override
                public void run(){
                    checkAndRemoveDuplicates();
                }}.start();
    }

    /**set size dialog attributes, such as size and position.*/
    private void setDialog(){
        // set dialog size
        int width=625; int height=378;
        this.setSize(new Dimension(width,height));

        // set dialog postion
        int w = gui.getWidth();
        int h = gui.getHeight();
        int guix=gui.getX(), guiy=gui.getY();

        int x=guix+(int)((w-width)/2);
        int y=guiy+(int)((h-height)/2);

        this.setLocation(x, y);

        // remove content in list
        Vector<String> list = new Vector();
        jList_msg.setListData(list);

        this.jProgressBar1.setValue(0);

    }

    /**set min and max value by return value from
     * ResultEditor.Annotations.Depot.getSizeofallannotations()
     */
    private void setProgressBar(){
        ResultEditor.Annotations.Depot depot = new ResultEditor.Annotations.Depot();
        int max=depot.getSizeOfAllAnnotations();
        this.__max = max;

        this.jProgressBar1.setMinimum(0);
        this.jProgressBar1.setMaximum(max);

        this.jLabel_msg.setText("Progress(0/"+max+"):");
    }

    private void checkAndRemoveDuplicates(){
        // ##1## check and remove duplicates
        ResultEditor.Annotations.Depot depot = new ResultEditor.Annotations.Depot();
        depot.removeDuplicates(this.jProgressBar1, RemoveDuplicates.jList_msg, this.jLabel_msg, __max);

        // ##2## refresh screen after removing duplicated annotations
        gui.repaintHighlighter();
        gui.showAnnotationCategoriesInTreeView_refresh();
        gui.showValidPositionIndicators();
        gui.display_SetStatusInsiable_ToComparisionPanel();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList_msg = new javax.swing.JList();
        jPanel5 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jProgressBar1 = new javax.swing.JProgressBar();
        jLabel_msg = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);

        jPanel1.setBackground(new java.awt.Color(226, 226, 226));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel3.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 51, 153), 2));

        jList_msg.setBackground(new java.awt.Color(240, 240, 255));
        jList_msg.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jList_msg.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Empty List" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList_msg.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jList_msg);

        jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel3, java.awt.BorderLayout.CENTER);

        jPanel5.setBackground(new java.awt.Color(226, 226, 226));

        jLabel1.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel1.setText("Check all annotations in memory and remove duplicates:");

        jLabel_msg.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel_msg.setText("Progress:");

        org.jdesktop.layout.GroupLayout jPanel5Layout = new org.jdesktop.layout.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jProgressBar1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 535, Short.MAX_VALUE)
            .add(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addContainerGap(215, Short.MAX_VALUE))
            .add(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel_msg)
                .addContainerGap(476, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .add(2, 2, 2)
                .add(jLabel_msg)
                .add(18, 18, 18)
                .add(jProgressBar1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.add(jPanel5, java.awt.BorderLayout.PAGE_START);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel2.setBackground(new java.awt.Color(167, 167, 167));
        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(167, 167, 167), 3));
        jPanel2.setLayout(new java.awt.BorderLayout());

        jButton1.setFont(new java.awt.Font("Calibri", 1, 13)); // NOI18N
        jButton1.setText("      OK      ");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton1, java.awt.BorderLayout.EAST);

        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    /*public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RemoveDuplicates().setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel_msg;
    private static javax.swing.JList jList_msg;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
